﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CFControls
{
    public partial class UnitValueControl : UserControl
    {
        public string ValueName { get; set; }
        public string Value { get; set; }
        public string Unit { get; set; }
        private Brush blackBrush = new SolidBrush(Color.Black);
        private Bitmap doubleBuffer;

        private Font valueNameFont;
        private Font unitFont;
        private Font valueFont;

        public Font ValueNameFont
        {
            get { return this.valueNameFont; }
            set { this.valueNameFont = value; }
        }

        public Font UnitFont
        {
            get { return this.unitFont; }
            set { this.unitFont = value; }
        }

        public Font ValueFont
        {
            get { return this.valueFont; }
            set { this.valueFont = value; }
        }

        int scale = 0;
        int halfScale = 0;

        public UnitValueControl()
        {
            this.ValueName = string.Empty;
            this.Value = string.Empty;
            this.Unit = string.Empty;
            InitializeComponent();
            this.doubleBuffer = new Bitmap(this.Width, this.Height);
            scale = Height / 3;
            halfScale = scale / 2;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.doubleBuffer == null)
            {
                doubleBuffer = new Bitmap(this.Width, this.Height);
            }
                
            Graphics graphics = Graphics.FromImage(doubleBuffer);
            graphics.Clear(this.BackColor);

            if (this.valueNameFont == null)
            {
                valueNameFont = new Font(FontFamily.GenericSansSerif, halfScale, FontStyle.Regular);
            }

            if (this.unitFont == null)
            {
                unitFont = new Font(FontFamily.GenericSansSerif, halfScale, FontStyle.Regular);
            }

            if (this.valueFont == null)
            {
                valueFont = new Font(FontFamily.GenericSansSerif, scale, FontStyle.Regular);
            }
            SizeF valueNameSize = graphics.MeasureString(ValueName, valueNameFont);
            Rectangle valueRectangle = new Rectangle(1, 1, (int)valueNameSize.Width + 4, (int)valueNameSize.Height + 1);
            graphics.DrawString(this.ValueName, valueNameFont, blackBrush, valueRectangle);

            SizeF unitSize = graphics.MeasureString(Unit, unitFont);
            Rectangle unitRectangle = new Rectangle(valueRectangle.Width, 1, (int)unitSize.Width + 1, (int)unitSize.Height + 1);
            graphics.DrawString(this.Unit, unitFont, blackBrush, unitRectangle);

            SizeF valueSize = graphics.MeasureString(Value, valueFont);
            Rectangle rectangle = new Rectangle(4, valueRectangle.Height, (int)valueSize.Width + 1, (int)valueSize.Height + 1);
            graphics.DrawString(this.Value, valueFont, blackBrush, rectangle);
            graphics.Dispose();
            e.Graphics.DrawImage(doubleBuffer, 0, 0);
            
            base.OnPaint(e);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }

        protected override void OnResize(EventArgs e)
        {
            scale = Height / 3;
            halfScale = scale / 2;
            base.OnResize(e);
        }
    }
}
